package com.hxx.controller;

import com.github.pagehelper.PageInfo;
import com.hxx.entily.Result;
import com.hxx.pojo.Member;
import com.hxx.service.IMemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("member")
public class MemberController {
    @Autowired
    private IMemberService memberService;

    /*
   去用户页面
   */
    @RequestMapping("memberlist")
    public  String memberlist(Model model, String name,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){

        List<Member> allMembers = memberService.getAllMembers(name, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allMembers);
        //System.out.println(pageInfo);
        model.addAttribute("allMember",allMembers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",name);
        return "memberlist";
    }

    /**
     * 用户新增
     * @return
     */
    @RequestMapping("/addMember")
    @ResponseBody
    public Result addMember(Member sysmember){
        Result<Object> result = new Result<>();
        if (sysmember!=null
                && sysmember.getName()!=null
                && !sysmember.getName().trim().equals("")
        ){
            System.out.println(sysmember);
            //是否重名
            Member member = memberService.getMemberByname(sysmember.getName());
            if (member!=null){
                result.setMsg("用户已经存在！");
                result.setSuccess(false);
                return result;
            }
            int i = memberService.addMember(sysmember);
            if (i>0){
                System.out.println("新增成功！");
                result.setMsg("新增成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("新增失败！");
                result.setMsg("新增失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名和密码！");
            result.setMsg("请输入用户名和密码！");
            result.setSuccess(false);
            return result;
        }

    }
    /**
     *
     * @return
     */
    /**
     * 删除用户
     */
    @RequestMapping("delMember")
    @ResponseBody
    public Result deleteMemberById(Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            int i=memberService.deleteMemberById(id);
            if(i>0){
                System.out.println("删除成功");
                result.setMsg("删除成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("删除失败");
                result.setMsg("删除失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("请选择要删除的ID！");
            result.setMsg("请选择要删除的ID");
            result.setSuccess(false);
            return   result;
        }

    }
    /**
     * 批量删除
     * @return
     */
    @RequestMapping("/delAll")
    @ResponseBody
    public Result delAll(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);

        if (ids!=null && !ids.trim().equals("")){
            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);

            int i = memberService.deleteMemberAll(idsList);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择id！");
            result.setMsg("请选择id！");
            result.setSuccess(false);
            return result;
        }

    }
    /**
     * 估计ID查用户
     */
    @RequestMapping("getMemberById")
    @ResponseBody
    public Result  getMemberById ( Integer id){
        Result<Object> result=new Result<>();
        if(id!=null){
            Member member = memberService.getMemberById(id);
            if(member!=null){
                result.setData(member);
                System.out.println("成功");
                result.setMsg("查询成功");
                result.setSuccess(true);
                return   result;
            } else {
                System.out.println("失败");
                result.setMsg("失败");
                result.setSuccess(false);
                return   result;
            }
        }else {
            System.out.println("没有此用户！");
            result.setMsg("请选择ID");
            result.setSuccess(false);
            return   result;
        }

    }
    /**
     * 修改数据
     * @return
     */
    @RequestMapping("/editMember")
    @ResponseBody
    public Result editMember(Member sysmember){
        Result<Object> result = new Result<>();
        if (sysmember!=null
                && sysmember.getName()!=null
                && !sysmember.getName().trim().equals("")
                && sysmember.getId()!=null
        ){
            int i = memberService.editMember(sysmember);
            if (i>0){
                System.out.println("修改成功！");
                result.setMsg("修改成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("修改失败！");
                result.setMsg("修改失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名和ID！");
            result.setMsg("请输入用户名和ID！");
            result.setSuccess(false);
            return result;
        }

    }

}
